package com.sc.sysconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.sysconfig.dao.SysLogMapper;
import com.sc.sysconfig.dto.LogQueryDTO;
import com.sc.sysconfig.entity.SysLog;
import com.sc.sysconfig.service.SysLogService;
import com.sc.sysconfig.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 系统日志服务impl
 *
 * @author 徐赫阳
 * @date 2021/09/05
 */
@Service
@Component
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {

    /**
     * 系统日志映射器
     */
    @Autowired
    SysLogMapper sysLogMapper;

    /**
     * 获取日志列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link SysLog}>
     */
    @Override
    public PageVO<SysLog> getLogList(LogQueryDTO queryDTO){
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(queryDTO.getUser() != null,SysLog::getUser,queryDTO.getUser())
                .orderByDesc(SysLog::getId);
        Integer count = this.sysLogMapper.selectList(queryWrapper).size();
        List<SysLog> list = this.sysLogMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }
}
